package com.wanmait.movie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.movie.entity.DTO.GenSeatMapDTO;
import com.wanmait.movie.entity.DTO.GenSeatMapDTOTwo;
import com.wanmait.movie.entity.DTO.seat.SeatDTO;
import com.wanmait.movie.entity.Seat;
import com.wanmait.movie.entity.request.manage.lobby.UpLobbySeatRequest;
import com.wanmait.movie.entity.request.manage.seat.RanksRequest;
import com.wanmait.movie.mapper.SeatMapper;
import com.wanmait.movie.service.SeatService;
import com.wanmait.movie.util.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* @author 24672
* @description 针对表【seat(座位表)】的数据库操作Service实现
* @createDate 2024-01-02 10:44:42
*/
@Service
public class SeatServiceImpl extends ServiceImpl<SeatMapper, Seat>
    implements SeatService{
    /**
     * 生成影厅座位
     * @param genSeatMapDTO 座位信息
     */
    @Override
    public boolean genSeatMap(GenSeatMapDTOTwo genSeatMapDTO) {
        /**
         * 生成一个完全的座位图，然后手动修改哪些位置不是座位
         */
        List<Seat> seats = new ArrayList<>();
        Integer currentAdminId = Integer.parseInt(JWTUtils.getAudience(genSeatMapDTO.getRequest().getHeader("token")));
        for (int i = 1; i <= genSeatMapDTO.getRow(); i++) {
            for (int j = 1; j <= genSeatMapDTO.getCol(); j++) {
                Seat seat = new Seat();
                seat.setSrow(i);
                seat.setScol(j);
                seat.setSeatNumber(i + "排" + j + "座");
                seat.setInsertAdminId(currentAdminId);
                seat.setUpdateAdminId(currentAdminId);
                seat.setLobbyId(genSeatMapDTO.getLobbyId());
                seats.add(seat);

            }
        }
        return this.saveBatch(seats);
    }

    /**
     * 获取座位图
     * @param ranksRequest
     * @return
     */
    @Override
    public List<List<Seat>> getSeatMap(RanksRequest ranksRequest) {
        Integer col = Integer.parseInt(ranksRequest.getCol());
        List<Seat> seats = this.query().eq("lobby_id", ranksRequest.getLobbyId()).list();
        List<List<Seat>> result = new ArrayList<>();
        for (int i = 0; i < seats.size(); i+=col){
            List<Seat> rols = new ArrayList<>();
            for (int j = i; j < i + col; j++) {
                rols.add(seats.get(j));
            }
            result.add(rols);
        }
       return result;
    }
}




